﻿Imports System.Data.SqlClient
Public Class Totalmoney
    Dim cmd As New SqlCommand

    Private Sub Totalmoney_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        condb()
        cmd.Connection = cn
        cmd.CommandText = "select top(1)InvoiceID from tblorder order by InvoiceID desc"
        txtinvoice.Text = cmd.ExecuteScalar

        cmd.CommandText = "select Totallpay from tblorder where InvoiceID ='" & txtinvoice.Text & "'"
        txttotalmoney.Text = cmd.ExecuteScalar

        txtafterdiscon.Text = txttotalmoney.Text

        Dim d As Date = Now
        txtdate.Text = d
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub

    Private Sub btnprint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprint.Click
        Dim frm As New Invoice
        frm.TopLevel = False
        Me.printinvoice.Controls.Add(frm)
        frm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        frm.WindowState = FormWindowState.Maximized
        frm.Show()
    End Sub

    Private Sub txtinvoice_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtinvoice.TextChanged
        cmd.CommandText = "select Totallpay from tblorder where InvoiceID ='" & txtinvoice.Text & "'"
        txttotalmoney.Text = cmd.ExecuteScalar
        txtafterdiscon.Text = cmd.ExecuteScalar
    End Sub

    Private Sub txtdiscon_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtdiscon.TextChanged
        Try
            Dim a, b, c, total As Double
            a = Val(txttotalmoney.Text)
            b = Val(txtdiscon.Text)
            c = a * b \ 100
            total = a - c
            txtafterdiscon.Text = total
        Catch ex As Exception
        End Try
    End Sub

    Private Sub txtgetmoney_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtgetmoney.TextChanged
        Try
            Dim a As Double

            If txtgetmoney.Text > 0 Then
                a = Val(txtgetmoney.Text) - Val(txtafterdiscon.Text)
                txttotalamount.Text = a
            Else
                txttotalamount.Text = 0
            End If
            If CDbl(txtgetmoney.Text) < CDbl(txtafterdiscon.Text) Then
                txttotalamount.Text = "លុយមិនគ្រប់ទេ!!"
                txtnotresievmoney.Text = txtafterdiscon.Text - txtgetmoney.Text
            Else
                txttotalamount.Text = a
                txtnotresievmoney.Text = 0
            End If
        Catch ex As Exception
        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        cmd.Connection = cn
        cmd.CommandText = "insert into TotallMoney values('" & txtinvoice.Text & "','" & txtdate.Text & "','" & txttotalmoney.Text & "','" & txtdiscon.Text & "','" & txtafterdiscon.Text & "','" & txtgetmoney.Text & "','" & txtnotresievmoney.Text & "')"
        cmd.ExecuteNonQuery()
    End Sub
End Class